(require 'emacs-wiki)
(require 'emacs-wiki-table)
(require 'emacs-wiki-menu)
(require 'emacs-wiki-srctag)

(setq emacs-wiki-directories '("~/wiki"))
(setq emacs-wiki-publishing-directory "~/publish")

(setq emacs-wiki-projects
      `(("FreeBSD" .
         ((emacs-wiki-directories . ("~/wiki/FreeBSD/"))
          (emacs-wiki-project-server-prefix . "../FreeBSD/")
          (emacs-wiki-publishing-directory . "~/publish/FreeBSD/")))

        ("Emacs" .
         ((emacs-wiki-directories . ("~/wiki/Emacs/"))
          (emacs-wiki-project-server-prefix . "../Emacs")
          (emacs-wiki-publishing-directory . "~/publish/Emacs/")))))


(setq emacs-wiki-coding-default (quote gb2312))
(setq emacs-wiki-charset-default "gb2312")

(setq emacs-wiki-maintainer "mailto:dazhiqian@gmail.com")


;;(setq emacs-wiki-default-page "WelcomePage")
;;(setq emacs-wiki-index-page "WiKiIndex")

(setq emacs-wiki-pretty-changelogs t)

(setq emacs-wiki-inline-relative-to 'emacs-wiki-publishing-directory)

(setq emacs-wiki-style-sheet
      "<link rel=\"stylesheet\" type=\"text/css\" href=\"../css/main.css\">")

(setq emacs-wiki-publishing-header "<lisp>(my-publishing-header)</lisp>")
(setq emacs-wiki-publishing-footer "<lisp>(my-publishing-footer)</lisp>")

(defun my-publishing-header ()
  (if (file-readable-p "header.wiki")
      (ignore (insert-file-contents "header.wiki"))
	"<?xml version=\"1.0\"?>
	<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"xhtml11.dtd\">
	<html xmlns=\"http://www.w3.org/1999/xhtml\">
  	<head>
    		<title><lisp>(emacs-wiki-page-title)</lisp></title>
    		<meta name=\"generator\" content=\"emacs-wiki.el\" />
    		<meta http-equiv=\"<lisp>emacs-wiki-meta-http-equiv</lisp>\"
          	      content=\"<lisp>emacs-wiki-meta-content</lisp>\" />
    		<link rev=\"made\" href=\"<lisp>emacs-wiki-maintainer</lisp>\" />
    		<lisp>emacs-wiki-style-sheet</lisp>
  	</head>
  	<body>
    	        <lisp>(when (boundp 'emacs-wiki-menu-factory)
                     (funcall emacs-wiki-menu-factory))</lisp>
    	<h1 id=\"top\"><lisp>(emacs-wiki-page-title)</lisp></h1>
    	<!-- Page published by Emacs Wiki begins here -->\n"
    ))
(defun my-publishing-footer ()
  (if (file-readable-p "footer.wiki")
  (ignore (insert-file-contents "footer.wiki"))
"
<!-- Page published by Emacs Wiki ends here -->
<div class=\"navfoot\">
<hr />
<table width=\"100%\" border=\"0\">
        <tr>
          <td align=\"left\">
				<a href=\"http://www.freebsd.org\"
					style=\"text-decoration: none;\">
					<img style=\"border:0;width:128px;height:52px\"
						alt=\"FreeBSD logo\"
						src=\"../images/freebsd.png\">
				</a>

            	<a href=\"http://www.gnu.org/software/emacs/emacs.html\"
               style=\"text-decoration: none;\">
              <img 	alt=\"Emacs logo\"
                   src=\"../images/emacs.png\">
            	</a>

            	<a href=\"http://www.emacswiki.org/\"
               style=\"text-decoration: none;\">
              <img 	alt=\"Emacswiki.org logo\"
                   src=\"../images/emacswiki.png\">
            	</a>

				<a href=\"http://www.apache.org/\"
   					style=\"text-decoration: none;\">
  					<img src=\"../images/apache_pb.gif\"
  					alt=\"apache logo\" />
				</a>

				<a href=\"http://www.modssl.org/\"
					style=\"text-decoration: none;\">
					<img src=\"../images/mod_ssl_sb.gif\" 
					alt=\"modssl logo\" /> 
				</a>

				<a href=\"http://www.openssl.org/\"
					style=\"text-decoration: none;\">
					<img src=\"../images/openssl_ics.gif\" 
					alt=\"openssl logo\" /> 
				</a>
          </td>
        </tr>
</table>

<table width=\"100%\" border=\"0\">
		<tr>
<td>

</td>
			<td align=\"center\">
				<a href=\"mailto:dazhiqian@gmail.com\">
					<img src=\"../images/gmail.png\"
					alt=\"gmail logo\" /> 
				</a>
			<lisp>
				(if emacs-wiki-current-file
  					(concat
  					\"<span class=\\\"footdate\\\">Updated: \"
   					(format-time-string emacs-wiki-footer-date-format
   					(nth 5 (file-attributes emacs-wiki-current-file)))
    				\"</span>\"))
    		</lisp>
			</td>
		</tr>
</table>
</div>
</body>
</html>\n"
 ))

(defun emacs-wiki-preview-source ()
  (interactive)
  (emacs-wiki-publish-this-page)
  (find-file (emacs-wiki-published-file)))

(defun emacs-wiki-preview-html ()
  (interactive)
  (emacs-wiki-publish-this-page)
  (browse-url (emacs-wiki-published-file)))

(defun emacs-wiki-unlink-toggle ()
  "Toggle <nop> string in the beginning of the current word, to
un/make a word emacs-wiki link. The current word depends on the
point: if the cursor is on a non-whitespace character, it's
considered a word surrounded by whitespace. If the cursor is on a
whitespace character, the next word is looked up. This way
addressing a word works intuitively after having arrived on the
spot using forward-word."
  (interactive)
  (save-excursion
    (with-current-buffer (current-buffer)
    (if (looking-at "[ \t]")
	(goto-char (- (re-search-forward "[A-Za-z<>]") 1))
      (goto-char (+ (re-search-backward "[^A-za-z<>]") 1)))
    (if (looking-at "<nop>")
	(delete-char 5)
      (insert "<nop>")))))
;;}}}

(add-hook 'emacs-wiki-mode-hook
  (lambda ()
    (define-key emacs-wiki-mode-map (kbd "C-c C-h") 'emacs-wiki-preview-html)
    (define-key emacs-wiki-mode-map (kbd "C-c C-c") 'emacs-wiki-preview-source)
    (define-key emacs-wiki-mode-map (kbd "C-c C-v") 'emacs-wiki-change-project)
    (define-key emacs-wiki-mode-map (kbd "C-c C-n") 'emacs-wiki-unlink-toggle)
  ))

(require 'planner)
  (global-set-key (kbd "<f9> p") 'planner-create-task-from-buffer)
  (setq planner-publishing-directory "~/publish/plan/" )
  (planner-calendar-insinuate)
  (setq planner-calendar-show-planner-files t)
